package com.zyl.Ecs.device.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.zyl.Ecs.common.domain.device.bean.Sensor;
import com.zyl.Ecs.common.domain.device.ext.PositionExt;
import com.zyl.Ecs.common.domain.device.ext.SensorExt;
import com.zyl.Ecs.common.domain.device.ext.SensorTypeExt;
import com.zyl.Ecs.common.exception.EcsException;
import com.zyl.Ecs.common.response.ResultCode;
import com.zyl.Ecs.common.utils.ObjectUtil;
import com.zyl.Ecs.device.mapper.SensorMapper;
import com.zyl.Ecs.device.mapper.ext.SensorExtMapper;
import com.zyl.Ecs.device.service.SensorService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @version v1.0
* @Package: com.zyl.Ecs.device.service.impl
* @ClassName: SensorServiceImpl
* @Description 针对表【ecs_sensor】的数据库操作Service实现
* @Author 翟玉龙
* @Date 2024-12-05 09:52:35
*/

@Service
@RequiredArgsConstructor
public class SensorServiceImpl extends MPJBaseServiceImpl<SensorMapper, Sensor>
    implements SensorService{

    private final SensorMapper sensorMapper;
    private final SensorExtMapper sensorExtMapper;

    /**
     * 修改传感器信息
     *
     * @param sensorExt 传感器信息
     * @return {@link SensorExt }
     * @throws EcsException 异常
     */
    @Override
    public SensorExt update(SensorExt sensorExt) throws EcsException {
        /* 检查id是否存在 */
        Integer id = sensorExt.getId();
        internalGet(id);

        /* 执行修改 */
        sensorExt.setDeleted(0);
        sensorMapper.updateById(sensorExt);

        return get(id);
    }

    /**
     * 查询全部传感器列表
     *
     * @return {@link List }<{@link SensorExt }>
     * @throws EcsException 异常
     */
    @Override
    public List<SensorExt> getList() throws EcsException {
        return findSensor(false);
    }

    /**
     * 查询未绑定点位的传感器列表
     *
     * @return {@link List }<{@link SensorExt }>
     * @throws EcsException 异常
     */
    @Override
    public List<SensorExt> listUnbound() throws EcsException {
        return findSensor(true);
    }


    /**
     * 条件附加查询传感器
     * @param flag true 未绑定 false 绑定
     * @return {@link List }<{@link SensorExt }>
     */
    public List<SensorExt> findSensor(boolean flag) {
        MPJLambdaWrapper<SensorExt> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAll(SensorExt.class)
                .selectAssociation(SensorTypeExt.class, SensorExt::getSensorType, map -> map
                        .result(SensorTypeExt::getName)
                )
                .selectAssociation(SensorTypeExt.class, SensorExt::getSensorType)
                .selectAssociation(PositionExt.class, SensorExt::getPosition)
                .leftJoin(SensorTypeExt.class, SensorTypeExt::getId, SensorExt::getSensorTypeId)
                .leftJoin(PositionExt.class, PositionExt::getId, SensorExt::getPositionId)
                .isNull(flag, SensorExt::getPositionId);

        return sensorExtMapper.selectList(wrapper);
    }

    /**
     * 根据id查询传感器信息
     * @param id 传感器id
     * @return {@link SensorExt }
     */
    private SensorExt get(Integer id) {
        return SensorExt.toExt(internalGet(id));
    }

    /**
     * 根据id查询传感器信息
     * @param id 传感器id
     * @return {@link Sensor }
     */
    private Sensor internalGet(Integer id) {
        Sensor record = sensorMapper.selectById(id);
        if (ObjectUtil.isNull(record)) {
            throw new EcsException(ResultCode.SENSOR_NOT_EXIST);
        }
        return record;
    }
}




